package com.hzj.controller.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzj.helper.HttpRequestHelper;
import com.hzj.pojo.UserInfo;
import com.hzj.pojo.vo.user.UserInfoQueryVo;
import com.hzj.service.PatientService;
import com.hzj.service.UserInfoService;
import com.hzj.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

/**
 * @author Aisling
 * @date 2021-05-12 14:47
 */

@RestController
@RequestMapping("/api/user/admin")
public class ApiAdminController {

    @Autowired
    UserInfoService uis;
    @Autowired
    PatientService ps;


    //用户列表
    @PostMapping("/list/{page}/{limit}")
    public Result findAll(@PathVariable Integer page, @PathVariable Integer limit, @RequestBody UserInfoQueryVo userInfoQueryVo) {
        IPage iPage = new Page(page, limit);
        //UserInfoQueryVo获取条件值
        String name = userInfoQueryVo.getKeyword(); //用户名称
        Integer status = userInfoQueryVo.getStatus();//用户状态
        Integer authStatus = userInfoQueryVo.getAuthStatus(); //认证状态
        String createTimeBegin = userInfoQueryVo.getCreateTimeBegin(); //开始时间
        String createTimeEnd = userInfoQueryVo.getCreateTimeEnd(); //结束时间
        //对条件值进行非空判断
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(name)) {
            boolean phoneNum = isEmail(name);
            if (phoneNum) {
                wrapper.like("phone",name);
            }else {
                wrapper.like("name",name);
            }
        }
        if(!StringUtils.isEmpty(status)) {
            wrapper.eq("status",status);
        }
        if(!StringUtils.isEmpty(authStatus)) {
            wrapper.eq("auth_status",authStatus);
        }
        if(!StringUtils.isEmpty(createTimeBegin)) {
            wrapper.ge("create_time",createTimeBegin);
        }
        if(!StringUtils.isEmpty(createTimeEnd)) {
            wrapper.le("create_time",createTimeEnd);
        }

        wrapper.select();
        IPage iPage1 = uis.page(iPage, wrapper);
        return Result.ok(iPage1);
    }

    public boolean isEmail(String str) throws PatternSyntaxException {
        String regExp = "^[A-Za-z\\d]+([-_.][A-Za-z\\d]+)*@([A-Za-z\\d]+[-.])+[A-Za-z\\d]{2,4}$";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(str);
        return m.matches();
    }

    //用户锁定
    @PostMapping("/lock/{id}/{status}")
    public Result lockUser(@PathVariable Integer id, @PathVariable Integer status) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id", id);
        UserInfo userInfo = uis.getById(id);
        //锁定 0
        userInfo.setStatus(status);
        boolean update = uis.update(userInfo, queryWrapper);
        if (!update) {
            return Result.fail();
        }
        return Result.ok();
    }

    //用户详情
    @PostMapping("/show/{id}")
    public Result userDetails(@PathVariable Integer id) {
        //查询用户信息
        UserInfo user = uis.getById(id);
        //查询就诊人信息
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("user_id",id);
        List list = ps.list(wrapper);
        Map map=new HashMap();
        map.put("userInfo",user);
        map.put("patientList",list);
        return Result.ok(map);
    }

    //认证审批
    @PostMapping("/approval/{id}/{authStatus}")
    public Result userApproval(@PathVariable Integer id, @PathVariable Integer authStatus) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id", id);
        UserInfo userInfo = uis.getById(id);
        userInfo.setAuthStatus(authStatus);
        boolean update = uis.update(userInfo, queryWrapper);
        if (!update) {
            return Result.fail();
        }
        return Result.ok();
    }

    //删除用户
    @PostMapping("/delectUserinfo/{id}")
    public Result deleteUserInfo(@PathVariable Integer id){
        boolean removeById = uis.removeById(id);
        if(!removeById){
            return Result.fail();
        }
        return Result.ok();
    }

    //获取传递过来用户信息
    public Map<String, Object> getReqMap(HttpServletRequest request) {
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        System.out.println("----------------" + paramMap);
        return paramMap;
    }



}
